﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Common;

namespace MockPrjTeam4
{
    public partial class OrganisationEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            hdfOrgId.Value = Request.QueryString["ID"];
            string popup = @"<script>
            function popup(url, width, height)
            {
                //var width = 500;
                //var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) 
                { 
                    newwin.focus() 
                }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "Mast", popup);
            }

            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Organisation org = OrganisationBiz.Search(id);
            if (!IsPostBack)
            {
                //Details 3
                lstProgrammes.DataSource = ProgrammeBiz.GetListProgrammeName();
                lstProgrammes.DataBind();
                lstServices.DataSource = ServiceBiz.GetListServiceName();
                lstServices.DataBind();

                //Details BU/Directorate
                DataTable Dir = new DataTable();
                if (chkInactiveDirectory.Checked)
                {
                    Dir = DirectorateBiz.GetTableDirectorates(true, id);
                }
                else
                {
                    Dir = DirectorateBiz.GetTableDirectorates(false, id);
                }
                if (Dir.Rows.Count == 0)
                {
                    lblMessageResultDirectory.Text = "No more results";
                }
                else
                {
                    lblMessageResultDirectory.Text = "";
                }
                gvListDir.DataSource = Dir;
                gvListDir.DataBind();

                //Details4
                gvPremise.DataSource = PremiseBiz.GetPremise();
                gvPremise.DataBind();

                //Details5
                if (chkIncludeInactiveDetails5.Checked)
                {
                    gvSupportingMaterials.DataSource = SupportingMaterialBiz.GetTableSupportingMaterial(true);
                    gvSupportingMaterials.DataBind();
                }
                else
                {
                    gvSupportingMaterials.DataSource = SupportingMaterialBiz.GetTableSupportingMaterial(false);
                    gvSupportingMaterials.DataBind();
                }

                //Details 1
                txtOrgName.Text = org.OrganisationName ;
                txtOrgShortDes.Text = org.ShortDescription;
                DataTable dt = OrganisationBiz.SearchTableOrg(id);
                DataRow row;
                if (dt != null)
                {
                    row = dt.Rows[0];
                    txtContactName.Text = row["ContactName"].ToString();
                    txtTypeOfBusiness.Text = row["BusinessName"].ToString();
                    txtSICCode.Text = row["SICCode"].ToString();
                    txtAddLine1.Text = row["AddressLine1"].ToString();
                    txtAddLine2.Text = row["AddressLine2"].ToString();
                    txtAddLine3.Text = row["AddressLine3"].ToString();
                    txtPostcode.Text = row["Postcode"].ToString();
                    txtTown.Text = row["TownName"].ToString();
                    txtCounty.Text = row["CountyName"].ToString();
                    txtCountry.Text = row["CountryName"].ToString();
                    hdfTypeOfBusinessId.Value = row["BusinessId"].ToString();
                    hdfAddressId.Value = row["AddressId"].ToString();
                    hdfTownId.Value = row["TownId"].ToString();
                    hdfCountyId.Value = row["CountyId"].ToString();
                    hdfCountryId.Value = row["CountryId"].ToString();
                }
                txtPhoneNumber.Text = org.PhoneNumber;
                if (org.PreferredOrganisation == true) chkPreOrg.Checked = true;
                else chkPreOrg.Checked = false;
                txtOrgFullDes.Text = org.FullDescription;
                txtFax.Text = org.Fax;
                txtEmail.Text = org.Email;
                txtWebAdd.Text = org.WebAddress;
                txtCharityNumber.Text = org.CharityNumber;
                txtCompanyNumber.Text = org.CompanyNumber;
                if (org.IsActive == true) chkIsActive.Checked = true;
                else chkIsActive.Checked = false;

                //Details 2
                foreach (ListItem item in cblOrgSpecicallism.Items)
                {
                    if (item.Value == org.OrganisationSpecialism)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblServiceDisabilitiesCapabilities.Items)
                {
                    if (item.Value == org.ServiceDisabilities)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblServiceBarriersCapabilities.Items)
                {
                    if (item.Value == org.ServiceBarriers)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblServiceBenifitsCapabilities.Items)
                {
                    if (item.Value == org.ServiceBenefits)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblServicePersonalCircumstancesCapabilities.Items)
                {
                    if (item.Value == org.ServiceCircumstances)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblServiceEthinicityCapabilities.Items)
                {
                    if (item.Value == org.ServiceEthnicity)
                    {
                        item.Selected = true;
                    }
                }
                foreach (ListItem item in cblAccreditation.Items)
                {
                    if (item.Value == org.Accreditation)
                    {
                        item.Selected = true;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Organisation org = new Organisation();
            org.OrganisationName = txtOrgName.Text;
            org.ShortDescription = txtOrgShortDes.Text;
            org.AddressId = Methods.ConvertNumber(hdfAddressId.Value);
            org.BusinessId = Methods.ConvertNumber(hdfTypeOfBusinessId.Value);
            org.PhoneNumber = txtPhoneNumber.Text;
            org.TownId = Methods.ConvertNumber(hdfTownId.Value);
            org.CountyId = Methods.ConvertNumber(hdfCountyId.Value);
            org.Countryid = Methods.ConvertNumber(hdfCountryId.Value);
            if (chkPreOrg.Checked) org.PreferredOrganisation = true;
            else org.PreferredOrganisation = false;
            if (hdfContact.Value == "")
            {
                Contacts ct = ContactBiz.GetContactById(org.ContactId);
                if (ct == null)
                    org.ContactId = -1;
                else
                    org.ContactId = org.ContactId;
            }
            else
                org.ContactId = Methods.ConvertNumber(hdfContact.Value);
            org.FullDescription = txtOrgFullDes.Text;
            org.Fax = txtFax.Text;
            org.Email = txtEmail.Text;
            org.WebAddress = txtWebAdd.Text;
            org.CharityNumber = txtCharityNumber.Text;
            org.CompanyNumber = txtCompanyNumber.Text;
            if (chkIsActive.Checked) org.IsActive = true;
            else org.IsActive = false;
            org.OrganisationSpecialism = "";
            org.ServiceDisabilities = "";
            org.ServiceBarriers = "";
            org.ServiceBenefits = "";
            org.ServiceCircumstances = "";
            org.ServiceEthnicity = "";
            org.Accreditation = "";
            for (int i = 0; i < cblOrgSpecicallism.Items.Count; i++)
            {
                if (cblOrgSpecicallism.Items[i].Selected)
                {
                    org.OrganisationSpecialism = cblOrgSpecicallism.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceDisabilitiesCapabilities.Items.Count; i++)
            {
                if (cblServiceDisabilitiesCapabilities.Items[i].Selected)
                {
                    org.ServiceDisabilities = cblServiceDisabilitiesCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceBarriersCapabilities.Items.Count; i++)
            {
                if (cblServiceBarriersCapabilities.Items[i].Selected)
                {
                    org.ServiceBarriers = cblServiceBarriersCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceBenifitsCapabilities.Items.Count; i++)
            {
                if (cblServiceBenifitsCapabilities.Items[i].Selected)
                {
                    org.ServiceBenefits = cblServiceBenifitsCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServicePersonalCircumstancesCapabilities.Items.Count; i++)
            {
                if (cblServicePersonalCircumstancesCapabilities.Items[i].Selected)
                {
                    org.ServiceCircumstances = cblServicePersonalCircumstancesCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceEthinicityCapabilities.Items.Count; i++)
            {
                if (cblServiceEthinicityCapabilities.Items[i].Selected)
                {
                    org.ServiceEthnicity = cblServiceEthinicityCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblAccreditation.Items.Count; i++)
            {
                if (cblAccreditation.Items[i].Selected)
                {
                    org.Accreditation = cblAccreditation.Items[i].Text;
                }
            }
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            org.OrganisationId = id;
            Organisation orgName = OrganisationBiz.SearchOrgName(txtOrgName.Text);
            if (orgName != null && org.OrganisationId != orgName.OrganisationId)
            {
                lblMessage.Text = txtOrgName.Text + " had in DB. Enter Organisation Name again";
            }
            else
            {
                OrganisationBiz.EditOrganisation(org);
                lblMessage.Text = "You editted " + txtOrgName.Text + " successfully";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/ListOrganisation.aspx");
        }

        protected void chkExpInt_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExpInt.Checked)
            {
                tpDetails3.Visible = true;
                tcDep.ActiveTab = tpDetails3;
            }
            else
            {
                tpDetails3.Visible = false;
            }
        }

        protected void lkbtnUrl_Click(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format("EditSupportingMaterial.aspx?ID={0}&OrgID={1}", e.CommandName, hdfOrgId.Value));
        }

        protected void btnCreateDetails5_Click(object sender, EventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            Response.Redirect(string.Format("AddSupportingMaterial.aspx?ID={0}",
                id));
        }
        /// <summary>
        /// Button Create for Directory (Tab BU/Directorates)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreateDirectory_Click(object sender, EventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            lblMessageResultDirectory.Text = "";
            Response.Redirect(string.Format("AddDirectorate.aspx?ID={0}", id));
        }

        /// <summary>
        /// Checkbox Include Inactive for Directory (Tab BU/Directorates)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkInactiveDirectory_CheckedChanged(object sender, EventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            DataTable Dir;
            if (chkInactiveDirectory.Checked)
            {
                Dir = DirectorateBiz.GetTableDirectorates(true, id);
            }
            else
            {
                Dir = DirectorateBiz.GetTableDirectorates(false, id);
            }
            if (Dir.Rows.Count == 0)
            {
                lblMessageResultDirectory.Text = "No more results";
            }
            else
            {
                lblMessageResultDirectory.Text = "";
            }
            gvListDir.DataSource = Dir;
            gvListDir.DataBind();
        }


        /// <summary>
        /// Paging for GridView Directory (Tab BU/Directorates)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvListDir_PageIndexedChanging(object sender, GridViewPageEventArgs e)
        {
            gvListDir.PageIndex = e.NewPageIndex;
            gvListDir.DataBind();
        }

        /// <summary>
        /// Filter for GridView Directory (Tab BU/Directorates)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Click(object sender, CommandEventArgs e)
        {
            int id = Methods.ConvertNumber(Request.QueryString["ID"]);
            DataTable Dir = new DataTable();
            bool inActive;
            if (chkInactiveDirectory.Checked) inActive = true;
            else inActive = false;
            string range = "";
            switch (e.CommandName)
            {
                case "SearchAll":
                    {
                        Dir = DirectorateBiz.GetTableDirectorates(true, id);
                        break;
                    }
                case "Search09":
                    {
                        Dir = DirectorateBiz.Search("[0-9]%", inActive, id);
                        break;
                    }
                case "SearchAE":
                    {
                        Dir = DirectorateBiz.Search("[A-E]%", inActive, id);
                        break;
                    }
                case "SearchFJ":
                    {
                        Dir = DirectorateBiz.Search("[F-J]%", inActive, id);
                        break;
                    }
                case "SearchKN":
                    {
                        Dir = DirectorateBiz.Search("[K-N]%", inActive, id);
                        break;
                    }
                case "SearchOR":
                    {
                        Dir = DirectorateBiz.Search("[O-R]%", inActive, id);
                        break;
                    }
                case "SearchSV":
                    {
                        Dir = DirectorateBiz.Search("[S-V]%", inActive, id);
                        break;
                    }
                case "SearchWZ":
                    {
                        Dir = DirectorateBiz.Search("[W-Z]%", inActive, id);
                        break;
                    }
            }
            if (Dir.Rows.Count == 0)
            {
                lblMessageResultDirectory.Text = "No more results";
            }
            else
            {
                lblMessageResultDirectory.Text = "";
            }
            gvListDir.DataSource = Dir;
            gvListDir.DataBind();
        }


        /// <summary>
        /// Change to active for a Directory (Tab BU/Directorates)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lkbtnDirName_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int DirId = Methods.ConvertNumber(e.CommandName);
                DirectorateBiz.SetDirActive(DirId);
            }  
            Response.Redirect(string.Format("EditDirectorate.aspx?ID={0}&OrgID={1}", e.CommandName, hdfOrgId.Value));
        }
    }
}